var app = getApp();
import { api, util, dialog } from '../../../../js/util'
import { router } from '../../../../js/router'
import { grouporder } from "../../../../js/modules/order.js"
import authorize from '../../../../js/modules/authorize.js'
import { $ecCountDown } from '../../../../components/index'
import {
  wholesaleSettings
} from "../../../../js/modules/gloab";
import BasePage from '../../../../js/common/base-page'
import { setStorageSync } from '../../../../js/compatible';
BasePage({
  pageKey:"myGroupDetail",

  /**
   * 页面的初始数据
   */
  data: {
    imgDomain: api.imgDomain,
    enableOss: api.enableOss,
    OrderInfo: {},
    OrderItem: [],
    Consignee: [],
    InvoiceInfo: [],
    DispatchInfo: [],
    IsHasInvoice: true,
    IsHasDispatch: true,
    noPayMoney: 0.00,
    batchShow: true,
    id:0,
    flag:1,
    quantityDigits: 2,
    priceDigits: 2,//价格小数位数
    headerBg:"",
    Currency:'',//货币符号
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //判断是否登录
    if (!authorize.isLogin('store')) {
      authorize.redirect('store');
      return;
    }
    let flag = options.flag || 1;
    if(options.id){
      this.setData({
        id:options.id,
        flag: flag,
        flag_des: flag == 1 ? "拼团" : "预售"
      });
    }
   
    this.pageKey = flag == 1 ? 'myGroupDetail' : 'myPresellDetail'
     wx.setNavigationBarTitle({
       title: (this.data.flag == 1 ? "拼团" : "预售")+"详情"
    });

    this.getDetail();
    this.getWholesaleConfig();
  },
  /**
   * 倒计时
   *  */
  setCountdown(time) {
    this.c1 = new $ecCountDown({
      date: time,
      render(date) {
        const days = this.leadingZeros(date.days, 2)
        const hours = this.leadingZeros(date.hours, 2)
        const min = this.leadingZeros(date.min, 2)
        const sec = this.leadingZeros(date.sec, 2)

        this.setData({
          c1: days,
          c2: hours,
          c3: min,
          c4: sec
        })
      },
    });
  },
  /**加载订单信息 */
  getDetail: function () {
    var that = this;
    grouporder.get_group_detail(that.data.id,that.data.flag, function (content){
 
      that.setData({
        OrderInfo: content,
        OrderItem: content.ProductInfo,
        InvoiceInfo: content.Invoice[0],
        Consignee: content.Consignee[0]
      });
      //如果待支付时，显示倒计时
      if (content.PayStatus != 1 || (content.PayStatus == 1 && content.Status == 2 && content.OrderPayStatus == 3)){
        var pay_end_time = content.PayEndTime ? content.PayEndTime : util.formatTime(new Date());
        that.setCountdown(pay_end_time);
      }
    });
  },
  /**加载配置项参数 */
  getWholesaleConfig() {
    var that = this;
    var config = wholesaleSettings.getConfig();
    that.setData({
      quantityDigits: config.quantityDigits,
      priceDigits: config.priceDigits
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      headerBg:api.titleColor||"#e56b14",
      Currency:api.Currency || '￥'
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    setStorageSync('tabindex', app.dns.centerNum);
    router.navigate({
      path: "center"
    });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
    this.getDetail();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 点击查看分批次发货信息
   */
  showBatch: function () {
    this.setData({
      batchShow: !this.data.batchShow
    })
  },
  /**
   * 支付
   */
  onPayEvent:function(e){
    var that = this, t = e.currentTarget.dataset.type;
    grouporder.onPay(that.data.id,t);
  },
  /**
   * 去商品清单
   */
  goGoodsList:function(){
    router.navigate({
      path:'orderGoodsList',
      params:{}
    })
  },
  copyAddress () {
    let consignee = this.data.Consignee || {}
    let text = ''
    text += consignee.Name
    if (consignee.Mobile) {
      text += ` ${consignee.Mobile}`
    }
    text += ` ${consignee.Province}`
    text += `${consignee.City}`
    text += `${consignee.Area}`
    if (consignee.Town) {
      text += `${consignee.Town}`
    }
    text += ` ${consignee.Address}`
    wx.setClipboardData({
      data: text,
      fail (err) {
        console.log(err)
      }
    })
  }
})